package qq2564874169.tuaotuao.fx.utils;


import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

public class StringUtil {
    public static String join(CharSequence delimiter, Object... items) {
        var list = new ArrayList<String>();
        for (Object item : items) {
            if (item instanceof Iterable iterable) {
                iterable.forEach(i -> list.add(String.valueOf(i)));
            } else if (item != null && item.getClass().isArray()) {
                var len = Array.getLength(item);
                for (int i = 0; i < len; i++) {
                    list.add(String.valueOf(Array.get(item, i)));
                }
            } else {
                list.add(String.valueOf(item));
            }
        }
        return String.join(delimiter, list);
    }

    public static boolean isNullOrEmpty(String str, boolean isTrim) {
        if (str == null) {
            return true;
        }
        if (isTrim) {
            return str.isBlank() && str.isEmpty();
        }
        return str.isEmpty();
    }

    public static boolean isNullOrEmpty(String str) {
        return isNullOrEmpty(str, true);
    }

    public static String trim(String input, String str) {
        return trimLeft(trimRight(input, str), str);
    }

    public static String trimLeft(String input, String prefix) {
        return trimLeft(input, prefix, false);
    }

    public static String trimLeft(String input, String prefix, boolean ignoreCase) {
        String str = input;
        if (ignoreCase) {
            str = input.toLowerCase();
            prefix = prefix.toLowerCase();
        }
        int rs = 0;
        int idx;
        while ((idx = str.indexOf(prefix, rs)) == rs) {
            rs = idx + prefix.length();
        }
        if (rs > 0) {
            return str.substring(rs);
        }
        return input;
    }

    public static String trimRight(String input, String suffix) {
        return trimRight(input, suffix, false);
    }

    public static String trimRight(String input, String suffix, boolean ignoreCase) {
        String str = input;
        if (ignoreCase) {
            str = input.toLowerCase();
            suffix = suffix.toLowerCase();
        }
        int rs = str.length() - suffix.length();
        int idx;
        while ((idx = str.indexOf(suffix, rs)) == rs) {
            rs = idx - suffix.length();
        }
        if (idx >= 0) {
            return str.substring(0, idx + 1);
        }
        return input;
    }

    public static String repeat(String item, int count, String split) {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            list.add(item);
        }
        return String.join(split == null ? "" : split, list);
    }

    public static String repeat(String item, int count) {
        return repeat(item, count, null);
    }

    public static boolean hasValue(String str) {
        return !isNullOrEmpty(str);
    }

    public static boolean equals(String str1, String str2) {
        if (str1 == null && str2 == null) {
            return true;
        }
        if (str1 != null && str2 != null) {
            return str1.equals(str2);
        }
        return false;
    }

    public static String padLeft(String str, int count, String s) {
        if (str.length() >= count) {
            return str;
        }
        return repeat(s, count - str.length()) + str;
    }

    public static String padLeft(String str, int count) {
        return padLeft(str, count, " ");
    }

    public static String padRight(String str, int count, String s) {
        if (str.length() >= count) {
            return str;
        }
        return str + repeat(s, count - str.length());
    }

    public static String padRight(String str, int count) {
        return padRight(str, count, " ");
    }

    /**
     * 格式化字符串，支持{}占位符
     * 示例：format("1{}3{}5", "2", 4),结果："12345"
     * 如果args是一个集合则会自动展开
     * 示例：format("1{}3{}5", new String[]{"2", "4"}),结果："12345"
     */
    public static String format(String template, Object... args) {
        var vals = new ArrayList<>();
        for (Object item : args) {
            if (item instanceof Iterable iterable) {
                iterable.forEach(i -> vals.add(i));
            } else if (item != null && item.getClass().isArray()) {
                var len = Array.getLength(item);
                for (int i = 0; i < len; i++) {
                    vals.add(Array.get(item, i));
                }
            } else {
                vals.add(item);
            }
        }
        String s = template;
        for (var item : vals) {
            s = s.replaceFirst("\\{}", item != null ? item.toString() : "");
        }
        return s;
    }

    /**
     * 反转字符串
     */
    public static String reverse(String str) {
        StringBuilder reversed = new StringBuilder();
        for (int i = str.length() - 1; i >= 0; i--) {
            reversed.append(str.charAt(i));
        }
        return reversed.toString();
    }
}
